<template>
  <!-- taxationForm 报单记录 -->
  <div style="margin-top: 15px;">
    <!-- ## 搜索 -->
    <el-row style="background-color: #FFFFFF;">
      <el-col :span="7">
        <el-form ref="form" label-width="80px" style="display:flex;margin-top:8px ">
          <el-form-item label="记录人">
            <el-input />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4" style="margin-top:8px">
        <el-button type="primary" plain>搜索</el-button>
      </el-col>
      <el-col :span="6" style="margin-top:8px">
        <el-button type="primary" plain @click="addDuty">添加报单</el-button>
      </el-col>
      <el-col :span="4" style="margin-top:8px">
        <el-button type="primary" @click="exportExcelSelect">导出报单</el-button>
      </el-col>
    </el-row>

    <!-- ## 表格 -->
    <el-table v-loading="loading" element-loading-text="正在加载中" :data="tableData" style="width: 99%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column width="80" type="index" label="序号" />

      <el-table-column label="记录人" width="180" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>ID: {{ scope.row.id }}</p>
            <p>记录人: {{ scope.row.inspector }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.inspector }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="报单日期" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.inspectionDate) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="报单内容" width="240" prop="remarks" />

      <el-table-column label="更新时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="info" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template v-slot:empty>
        <div style="height: 200px;">
          <img src="../../assets/nodata.png" width="150" style="margin: 30px;">
        </div>
      </template>
    </el-table>
    <!-- ## 分页 -->
    <el-pagination
      :current-page.sync="currentPage3"
      :page-size="12"
      layout="prev, pager, next, jumper"
      :total="12"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- ## 弹层 -->
    <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="dialogVisible" width="50%">
      <div>
        <el-form ref="formDateRef" :rules="rules" label-position="right" label-width="80px" :model="formDate">
          <el-form-item label="报单人" prop="inspector">
            <el-input v-model="formDate.inspector" />
          </el-form-item>
          <el-form-item label="报单日期" prop="inspectionDate">
            <el-col :span="11">
              <el-date-picker v-model="formDate.inspectionDate" type="datetime" placeholder="选择日期时间" />
            </el-col>
          </el-form-item>

          <el-form-item label="报单内容" prop="remarks">
            <el-input v-model="formDate.remarks" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="verBtn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- ## 预览 -->
    <el-dialog title="表格保存预览" width="70%" :visible.sync="selectWindow">
      <el-table id="tableData" :data="selectData" height="380px">
        <el-table-column width="80" type="index" label="序号" />
        <el-table-column prop="inspectionDate" label="值班日期" />
        <el-table-column prop="inspector" label="姓名" />

        <el-table-column prop="serverName" label="服务器IP" />
        <el-table-column label="状态" width="180" align="center">
          <template slot-scope="scope">
            <el-tag size="medium" :type="scope.row.status === 0 ? 'success' : 'danger'">
              {{ scope.row.status === 0 ? '正常' : '异常' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注信息" />
        <el-table-column label="更新时间" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.updatedAt }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exportExcel">确定导出</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { getPatrol, addPatrol, changePatrol, delPatrol } from '@/api/patrol.js'
import htmlToExcel from '@/utils/htmlToExcel.js'
import { formatDate } from '@/utils/index.js'

export default {
  name: 'OperationDuty',
  data() {
    return {
      tableData: [], // 表格数据
      selectData: [],
      // 添加 编辑数据
      formDate: {
        inspector: '', // 巡检人
        serverName: '', // 主机名
        inspectionDate: '', // 巡检日期
        status: '', // 主机状态（0正常；1异常）
        remarks: '' // 备注信息
      },
      rules: {
        inspector: [{ required: true, message: '请输入报单人', trigger: 'blur' }],
        inspectionDate: [{ required: true, message: '请选择报单日期', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入报单内容', trigger: 'blur' }]
      },
      dialogVisible: false, // 弹层是否开启
      isEdit: true, // 是否是编辑
      currentPage3: 1, // 当前页数
      selectWindow: false,
      loading: true

    }
  },
  mounted() {
    this.getDutyData()
  },
  methods: {
    formatDate,
    // 获取表格数据
    async getDutyData() {
      const res = await getPatrol()
      this.tableData = res
      this.loading = false
    },
    // 新增
    addDuty() {
      this.formDate = {
        inspectionDate: '',
        inspector: '',
        remarks: '',
        serverName: '',
        status: ''
      }
      this.dialogVisible = true
      this.isEdit = false
    },
    // 编辑
    handleEdit(row) {
      console.log('id', row.id)
      this.dialogVisible = true
      this.isEdit = true
      this.formDate = {
        id: row.id,
        inspectionDate: row.inspectionDate,
        inspector: row.inspector,
        remarks: row.remarks,
        serverName: row.serverName,
        status: row.status
      }
    },
    // 删除
    handleDelete(row) {
      console.log('id', row.id)
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPatrol(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getDutyData()
      })
    },
    // 弹层 取消
    cancelBtn() {
      this.dialogVisible = false
    },
    // 弹层 确认
    async verBtn() {
      // 编辑-确认
      if (this.isEdit) {
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            // 编辑接口
            console.log(this.formDate)
            await changePatrol(this.formDate)
            this.$message({
              message: '编辑成功！',
              type: 'success'
            })
            this.getDutyData()
            this.dialogVisible = false
          }
        })

        // 添加-确认
      } else {
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            // 添加接口
            await addPatrol(this.formDate)
            this.$message({
              message: '添加成功！',
              type: 'success'
            })
            this.getDutyData()
            this.dialogVisible = false
          }
        })
      }
    },
    // 分页-每页条数
    handleSizeChange(pagesize) {
      console.log(pagesize)
    },
    // 分页-页码
    handleCurrentChange(page) {
      console.log(page)
    },
    // 导出
    exportExcel() {
      htmlToExcel.getExcel('#tableData', '巡检记录')
    },
    exportExcelSelect() {
      if (this.selectData.length < 1) {
        this.$message.error('请选择要导出的内容！')
        return false
      }
      this.selectWindow = true
    },
    handleSelectionChange(val) {
      this.selectData = val
    }
  }
}
</script>

<style lang="scss" scoped></style>
